import 'package:flutter/material.dart';
import 'package:flutter_screenutil/src/size_extension.dart';

import '../r.dart';
import 'gallery/gallery.dart';

/**
 * @Description: 预览 网格视频
 * @Author: 下雨了
 * @CreateDate: 2022-05-31 15:01
 * @UpdateUser: 更新者
 * @UpdateDate:  2022-05-31 15:01
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
///
class PreviewGridView extends StatefulWidget {
  final List<String> thumbnailPath;
  final List<String> sourcePath;
  final int crossAxisCount;
  final double spacing;

  const PreviewGridView({
    Key? key,
    required this.thumbnailPath,
    required this.sourcePath,
    this.crossAxisCount = 3,
    this.spacing = 10,
  }) : super(key: key);

  @override
  _PreviewGridViewState createState() => _PreviewGridViewState();
}

class _PreviewGridViewState extends State<PreviewGridView> {
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: widget.crossAxisCount,
          crossAxisSpacing: widget.spacing,
          mainAxisSpacing: widget.spacing,
          childAspectRatio: 1,
        ),
        itemBuilder: (context, index) {
          return GestureDetector(
            child: Stack(
              children: [
                Container(
                  width: 120.r,
                  height: 120.r,
                  child: Image.network(widget.thumbnailPath[index],
                    fit: BoxFit.cover,
                  ),
                  color: Colors.white,
                ),
                Visibility(
                  visible: widget.sourcePath[index].contains("mp4"),
                  child: Center(
                    child: Image.asset(R.assetsImgIcon57, width: 30.r,),
                  ),
                ),
              ],
            ),
            onTap: () {
              List<GalleryModel> list = [];
              widget.sourcePath.forEach((element) {
                list.add(GalleryModel(id: widget.sourcePath.indexOf(element).toString(), url: element));
              });
              gallery(list, showIndex: index);
            },
          );
        },
        itemCount: widget.thumbnailPath.length,
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics());
  }
}
